﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

namespace SharingCenter.Web.Classes
{
    using SharingCenter.Web.Classes.Groups;
    using SharingCenter.Web.Classes.enums;
    public class GroupAvatarImage : Image
    {
        protected static Guid _groupId;
        static bool groupidSet = false;
        protected static string _userName = "";
        protected static AvatarType _type = AvatarType.Normal;
        public bool AddHash { get; set; }

        public Guid GroupId 
        {
            get { return (Guid)ViewState["GID"]; }
            set 
            {
                ViewState["GID"] = value;
                _groupId = value;
                groupidSet = true;
            }
        }

        public AvatarType AvatarType 
        {
            get { return _type; }
            set { _type = value; }
        }

        

        public GroupAvatarImage()
            : base()
        {
            
        }

        protected override void  Render(System.Web.UI.HtmlTextWriter writer)
        {
 	         
             if (!groupidSet)
             {
                 throw new Exception("GroupId must be set.");
             }

             try
             {
                 this.CssClass += " nom";
                
                 Sc_Group g = GroupOperations.GetGroupByGroupId(GroupId);

                 string avatarmagePath = string.Empty;

                 if (g.AvatarImage == null)
                 {
                     avatarmagePath = _type == AvatarType.Normal ? WebConsts.G_AVATAR_DEFAULT_IMG_PATH : WebConsts.G_AVATAR_DEFAULT_IMG_THUMBNAIL_PATH;
                     var format = AddHash ? "{0}?{1}" : "{0}";
                     this.ImageUrl = string.Format(format, avatarmagePath, DateTime.Now.Ticks);
                 }
                 else
                 {
                     avatarmagePath = g.AvatarImage.Element(_type.ToString()).Value;
                     var format = AddHash ? "{0}/{1}?{2}" : "{0}/{1}";
                     this.ImageUrl = string.Format(format, WebConsts.AVATAR_FOLDER, avatarmagePath, DateTime.Now.Ticks);
                 }
             }
             catch { }

             base.Render(writer);
        }

       
    }
}
